home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is Simple Timer.
- *
- * The Initial Developer of the Original Code is
- * George Bradt.
- *
- * Portions created by the Initial Developer are Copyright (C) 2009
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *
- * Credits:
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
- var SimpleTimerEventLog = {
- separator: "\x1D",
-
- // Called when loading Event Log dialog.
-
- onLoadEventLog: function() {
- var strbundle = document.getElementById("simtim-strings");
-
- try {
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].
- getService(Components.interfaces.nsIPrefService).
- getBranch("extensions.simpletimer@grbradt.org.");
- var eventLogPath = prefs.getComplexValue("eventLogPath",
- Components.interfaces.nsILocalFile).path;
- var deleteLogEntries = prefs.getIntPref("deleteLogEntries");
- }
- catch(e) {
- alert(strbundle.getString("alert.error.get.prefs") +
- "\n" + e.message);
- return;
- }
-
-
- var json = Components.classes["@mozilla.org/dom/json;1"].
- createInstance(Components.interfaces.nsIJSON);
-
- var strArray = [];
- var logArray = [];
- var file = SimpleTimerFileIO.open(eventLogPath);
-
- if ( file && file.exists() ) {
- var treeChildren = document.getElementById("simtim-treeItems");
- var inData = SimpleTimerFileIO.read(file);
-
- if ( inData ) {
- // Calculate entry delete time. deleteLogEntries is days to retain an entry,
- // and a value of 0 means no deleting.
- if ( deleteLogEntries ) {
- var currTimeMilli = new Date().getTime();
- var lifeSpanMilli = deleteLogEntries * ( 24 * 60 * 60 * 1000 );
- }
-
- // Strip last seperator.
- inData = inData.slice(0, -1);
-
- // An array of JSON strings.
- strArray = inData.split(this.separator);
-
- // Update log file if entries are dropped.
- var outData = "";
- var entriesDropped = false;
-
- for ( var i in strArray ) {
- try {
- logArray[i] = json.decode(strArray[i]);
- }
- catch(e) {
- window.opener.alert(strbundle.getString("alert.error.log.invalid.data.in") + "\n" +
- strArray[i]);
- continue;
- }
-
- if ( !deleteLogEntries ||
- ( logArray[i].logTimeMilli + lifeSpanMilli ) > currTimeMilli ) {
- this.addTreeRow(treeChildren, logArray[i]);
- outData += strArray[i] + this.separator;
- }
- else {
- entriesDropped = true;
- }
- }
-
- if ( entriesDropped ) {
- // Overwrite log file.
- if ( !SimpleTimerFileIO.write(file, outData, "") ) {
- window.opener.alert(strbundle.getString("alert.error.log.file.write") + "\n" +
- eventLogPath);
- }
- }
- }
- // Empty log file also drops here.
- else if ( inData === false ) {
- window.opener.alert(strbundle.getString("alert.error.log.file.read") + "\n" +
- eventLogPath);
- }
- }
- else {
- window.opener.alert(strbundle.getString("alert.error.log.file.open") + "\n" +
- eventLogPath);
- }
- },
-
- // Add a logged event to the tree.
-
- addTreeRow: function(treeChildren, data) {
- var item = document.createElement("treeitem");
- var row = document.createElement("treerow");
- var cell;
-
- // First cell is "logDate".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.logDate);
- row.appendChild(cell);
-
- // Second cell is "logTime".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.logTime);
- row.appendChild(cell);
-
- // Third cell is "type".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.type);
- row.appendChild(cell);
-
- // Fourth cell is "eventTime".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.eventTime);
- row.appendChild(cell);
-
- // Fifth cell is "recurring".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.recurring);
- row.appendChild(cell);
-
- // Sixth cell is "status".
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.status);
- row.appendChild(cell);
-
- // Seventh cell is "descr", the optional description.
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.description);
- row.appendChild(cell);
-
- // Eighth cell is "url", the optional url for notifications.
- cell = document.createElement("treecell");
- cell.setAttribute("label", data.url);
- row.appendChild(cell);
-
- // Add the treerow onto treeitem, append item.
- item.appendChild(row);
- treeChildren.appendChild(item);
- },
-
- logEvent: function(type, eventTime, recurring, status, description, url) {
- var strbundle = document.getElementById("simtim-strings");
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
- getService(Components.interfaces.nsIWindowMediator);
- var win = wm.getMostRecentWindow("navigator:browser");
-
- // Compare window references.
- if ( win.window != window.window ) {
- // Only log once.
- return;
- }
-
- var json = Components.classes["@mozilla.org/dom/json;1"].
- createInstance(Components.interfaces.nsIJSON);
-
- var file = SimpleTimerFileIO.open(SimpleTimer.eventLogPath);
-
- if ( file && file.exists() ) {
- var currDate = new Date();
-
- var logData = {logDate: currDate.toLocaleDateString(),
- logTime: currDate.toLocaleTimeString(),
- logTimeMilli: currDate.getTime(),
- type: type,
- eventTime: eventTime,
- recurring: recurring,
- status: status,
- description: description,
- url: url
- };
-
- try {
- var data = json.encode(logData) + this.separator;
- }
- catch(e) {
- alert(strbundle.getString("alert.error.log.invalid.data.out") + "\n" +
- logData);
- return;
- }
-
- // Append to log file.
- if ( !SimpleTimerFileIO.write(file, data, "a") ) {
- alert(strbundle.getString("alert.error.log.file.write") + "\n" +
- eventLogPath);
- }
- }
- else {
- alert(strbundle.getString("alert.error.log.file.open") + "\n" +
- eventLogPath);
- }
- },
-
- // Called when clicking Clear button in Event Log dialog.
-
- onClear: function() {
- var strbundle = document.getElementById("simtim-strings");
- var file = SimpleTimerFileIO.open(window.opener.SimpleTimer.eventLogPath);
-
- if ( file && file.exists() ) {
- var inData = SimpleTimerFileIO.read(file);
-
- if ( inData ) {
- // Get confirmation.
- var button = confirm(strbundle.getString("confirm.clear.log.file"));
-
- if ( button ) {
- // User clicked OK. Overwrite log file.
- if ( !SimpleTimerFileIO.write(file, "", "") ) {
- alert(strbundle.getString("alert.error.log.file.write") + "\n" +
- eventLogPath);
- }
- else {
- // Delete tree items.
- var treeChildren = document.getElementById("simtim-treeItems");
-
- // Remove from the bottom.
- while ( treeChildren.hasChildNodes() ) {
- treeChildren.removeChild(treeChildren.lastChild);
- }
- }
- }
- }
- else if ( inData === false ) {
- alert(strbundle.getString("alert.error.log.file.read") + "\n" +
- eventLogPath);
- }
- }
- else {
- alert(strbundle.getString("alert.error.log.file.open") + "\n" +
- eventLogPath);
- }
- },
-
- // Called on first-run at startup, or when resetting from Options dialog.
-
- createDefaultLogFile: function() {
- var strbundle = document.getElementById("simtim-strings");
- var file = Components.classes["@mozilla.org/file/directory_service;1"].
- getService(Components.interfaces.nsIProperties).
- get("ProfD", Components.interfaces.nsIFile);
-
- file.append("SimpleTimer");
- file.append("eventLog.txt");
-
- if ( !file.exists() ) {
- if ( SimpleTimerFileIO.create(file) ) {
- SimpleTimer.prefs.setComplexValue("eventLogPath", Components.interfaces.nsILocalFile, file);
- }
- else {
- alert(strbundle.getString("alert.error.log.file.create") + "\n" +
- file.path);
- return false;
- }
- }
-
- return file;
- },
-
- // Debug messages to console.
-
- debug: function(aMsg) {
- setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
- }
- };